package com.atmilan.quartz.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.atmilan.quartz.mapper.BizMerchantMapper;
import com.atmilan.quartz.mapper.MerchantMapper;
import com.atmilan.quartz.model.dto.buyer.MerchantDTO;
import com.atmilan.quartz.model.entity.buyer.Merchant;
import com.atmilan.quartz.model.entity.buyer.MerchantExample;
import com.atmilan.quartz.service.IMerchantService;
import com.atmilan.quartz.utils.BeanConvert;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@Transactional
public class MerchantServiceImpl implements IMerchantService{
	@Autowired
	MerchantMapper merchantMapper;
    @Autowired
    BizMerchantMapper bizMerchantMapper;

	@Override
	public Map<Long, Merchant> getAll() {
		return null;
	}

	// 10秒缓存, 加本地缓存,降低db压力
	TimedCache<Long, Merchant> timedCache = CacheUtil.newTimedCache(60*10000);
	@Override
	public Merchant getById(Long id) {
		Merchant mt = timedCache.get(id);
		if(mt==null){
			mt = merchantMapper.selectByPrimaryKey(id);
			if(mt!=null){
				timedCache.put(id,mt);
			}
		}
		return mt;
	}

	@Override
	public List<Merchant> getByContactsMobilePhone(String contactsMobilePhone) {

		MerchantExample merchantExample=new MerchantExample();
		merchantExample.createCriteria().andContactsMobilePhoneEqualTo(contactsMobilePhone);

		return merchantMapper.selectByExample(merchantExample);
	}

	@Override
	public List<Merchant> getByMerchantName(String merchantName) {
		MerchantExample merchantExample=new MerchantExample();
		merchantExample.createCriteria().andMerchantNameEqualTo(merchantName);

		return merchantMapper.selectByExample(merchantExample);
	}

	/**
	 * 供应商的 上下级关系 改变 或者 加价率改变 的时候调用一次
	 * @param merchantId
	 * @return
	 */
	boolean updateMerchantSupplierRelation(Long merchantId){
		Merchant merchant=	merchantMapper.selectByPrimaryKey(merchantId);
		//获取他本身有的供应商   他本身有的供应商加价率 是0



	//	get(merchantId);

		return true;
	}

	/**
	 *
	 * @param parentId
	 * @param merchantId 给这个商户 添加 parentId
	 * @return
	 */
	@Override
	public boolean addParentIds(Long parentId, Long merchantId) {
		Merchant merchant=	merchantMapper.selectByPrimaryKey(merchantId);
		String parentIds=merchant.getParentIds();

		if(parentIds!=null&&parentIds.length()>0) {
			String[] parentIdsArr = parentIds.split(",");
				//判断是否包含
			 boolean b=	Arrays.asList(parentIdsArr).contains(parentId + "");
			 if(!b ){
				 merchant.setParentIds(parentIds+","+parentId);
			 }else{
			 	log.info("merchantId"+merchantId+"已经包含："+parentId);
			 }
		}else{
			//第一次设置
			merchant.setParentIds(parentIds);
		}



		return merchantMapper.updateByPrimaryKeySelective(merchant)>0;
	}

	public	List<Merchant>	 getByParentIds(String parentIds){
		MerchantExample merchantExample=new MerchantExample();
		List<String> listString = Arrays.asList(parentIds.split(","));
		merchantExample.createCriteria().andParentIdsIn(listString);
		return  merchantMapper.selectByExample(merchantExample);
	}

	public	 String getParentMerchantNamesByParentIds(String parentIds){
		if(parentIds==null||parentIds.equals("0")||parentIds.equals("")){
			return "";
		}
		MerchantExample merchantExample=new MerchantExample();
		List<String> listString = Arrays.asList(parentIds.split(","));
		List<Long> listIds=new ArrayList<>();
		for (String id:listString){
			if(!id.equals("")){
				listIds.add(Long.valueOf(id));
			}

		}
		if(listIds.size()>0) {
			merchantExample.createCriteria().andIdIn(listIds);
			String parentMerchantNames = "";
			List<Merchant> merchants = merchantMapper.selectByExample(merchantExample);

			for (Merchant merchant : merchants) {
				parentMerchantNames += merchant.getMerchantName() + ",";
			}
			if (parentMerchantNames.length() > 2) {
				parentMerchantNames = parentMerchantNames.substring(0, parentMerchantNames.length() - 1);
			}
			return parentMerchantNames;
		}else{
			return "";
		}
	}

	@Override
	public boolean update(MerchantDTO merchantDTO) {
		boolean succ =  merchantMapper.updateByPrimaryKeySelective(BeanConvert.copyBean(merchantDTO,Merchant.class))>0;
		timedCache.remove(merchantDTO.getId());
		return succ;
	}

	@Override
	public boolean deleteById(Long id) {
		boolean succ =  merchantMapper.deleteByPrimaryKey(id)>0;
		timedCache.remove(id);
		return succ;
	}
}
